$(function () {
	//修改车区
	$("#btn_carareaUpdate").on("click", function(){
		var carareaId = $("input[name='carareaId']").val();
		var parentName = $("input[name='manageObject']").val();
		carareaUpdate(carareaId, parentName);
	});
	
	//导入车位
	$("#btn_importCararea").on("click", function(){
		alert("导入车位");
	});
	
	//删除车区
	$("#btn_carareaDel").on("click", function(){
		var carareaId = $("input[name='carareaId']").val();
		del(carareaId);
	});
	
	
});

//车区修改弹出层
function carareaUpdate(carareaId, parentName){
	JY.Ajax.doRequest(null,jypath+'/estate/projectCararea/view',{id:carareaId},function(data){
		setCarareaForm(data);
		$("#carareaForm label[name='parentName']").text(parentName);
    	save("编辑车区","/estate/projectCararea/edit", carareaId);
	});
}

function save(title, url, id){
	JY.Model.edit("carareaDiv",title,function(){
		if(JY.Validate.form("carareaForm")){
			var that =$(this);
			JY.Ajax.doRequest("carareaForm",jypath +url,null,function(data){
				that.dialog("close");
	        	JY.Model.info(data.resMsg,function(){
	        		var url = jypath + "/estate/projectCararea/detail?id="+id;
	        		$.post(url, null, function(data){
	        			$("#rightContentDiv").html(data);
	        		});
        		});    
			 });
		}	
	});
}

function del(id){
	JY.Model.confirm("该操作将同时删除车区下的所有车位,是否确认删除？",function(){
		JY.Model.confirm("请再次确认是否删除该车区？",function(){	
			JY.Ajax.doRequest(null,jypath+'/estate/projectCararea/delete',{id:id},function(data){
				JY.Model.info(data.resMsg,function(){
					$("#rightContentDiv").html("");
	 				if(data.res == 1){
	 	 				deleteNode();
	 	 			}
				});
			});
		});
	});
}

function setCarareaForm(data){
	var obj = data.obj;
	$("#carareaForm input[name$='id']").val(JY.Object.notEmpty(obj.id));
	$("#carareaForm input[name$='carareaName']").val(JY.Object.notEmpty(obj.carareaName));
	$("#carareaForm input[name$='carareaCode']").val(JY.Object.notEmpty(obj.carareaCode));
	$("#carareaForm textarea[name$='remark']").val(JY.Object.notEmpty(obj.remark));
}

